<template>
<!-- 给首页、分类页、购物车、用户我的页面写的共工的底部切换路由组件 -->
  <div class="tabbar">
    <van-tabbar v-model="select" active-color='red' @change="isTabbar">
      <van-tabbar-item icon-prefix="icon" icon="shouye">首页</van-tabbar-item>
      <van-tabbar-item icon-prefix="icon" icon="lipin">分类</van-tabbar-item>
      <van-tabbar-item v-if="$store.getters.getBadgeConst>0" icon-prefix="icon" icon="gouwuche" :badge="$store.getters.getBadgeConst">购物车</van-tabbar-item>
      <van-tabbar-item v-else icon-prefix="icon" icon="gouwuche">购物车</van-tabbar-item>
      <van-tabbar-item icon-prefix="icon" icon="wode">我的</van-tabbar-item>
    </van-tabbar>
  </div>
</template>

<script>
import Vue from 'vue'
import { Tabbar , TabbarItem , Icon} from 'vant'
Vue.use(Tabbar).use(TabbarItem).use(Icon)

export default {
  props:{
    active:{
      type:Number,
      default:0
    }
  },
  data(){
    return {
      select:0,
    }
  },
  mounted(){
    this.select = this.active
    // console.log(this.select);
  },      
  methods:{
    isTabbar(index){
      if(index === 0){
        this.$router.push('/home').catch(()=>{})
      }else if(index === 1){
        this.$router.push('/category').catch(()=>{})
      }else if(index === 2){
        // router.push是一个promise方法由于被路由导航拦截需要抛出一个异常
        this.$router.push('/shopcart').catch(()=>{})
      }else if(index === 3){
        this.$router.push('/user').catch(()=>{})
      }
    }
  }
}
</script>

<style>
  /* 允许穿透 允许给vant icon组件设置样式*/
  .van-tabbar-item__icon .icon{
    font-size: inherit;
  }
</style>